﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure;

namespace RegCustomer
{
    public class CommandHandlers
    {
        private readonly IRepository<Customer> _repository;

        public CommandHandlers(IRepository<Customer> repository)
        {
            _repository = repository;
        }

        public void Handle(CreateNewCustomerCommand command)
        {
            Customer customer = new Customer(command.PersonId, command.Name);
            _repository.Save(customer, command.OriginalVersion);
        }

        public void Handle(UpdateCustomerCommand command)
        {
            var customer = _repository.GetById(command.PersonId);             

            customer.UpdateName(command.NewName);
            _repository.Save(customer, command.OriginalVersion);
        }

        public void Handle(DeactivateCustomerCommand command)
        {
            var customer = _repository.GetById(command.PersonId);
            customer.MarkCustomerAsInvalid(command.PersonId, command.Reason);
            _repository.Save(customer, command.OriginalVersion);
        }

        public void Handle(MakeCustomerPriorityCommand command)
        {
            var customer = _repository.GetById(command.PersonId);
            customer.MakeCustomerPriorityCommand(command.PersonId);
            _repository.Save(customer, command.OriginalVersion);
        }

        public void Handle(UpdateCustomerDateOfBirth command)
        {
            var customer = _repository.GetById(command.PersonId);
            customer.UpdateDateOfBirth(command.PersonId, command.DateOfBirth);
            _repository.Save(customer, command.OriginalVersion);
        }
    }
}
